# usage: arm-none-eabi-gdb rt-thread.elf -x jtag_debug.sh

target remote localhost:2331
load
monitor reset
monitor halt

# set logging file out.txt
set logging on
set print pretty on
set pagination off
delete breakpoints
undisplay


define go_to_app
    # SCB->VTOR
    set $AppAddr=0xc0000000
    set $SCB_VTOR=0xE000ED08
    set *$SCB_VTOR=$AppAddr

    # set MSP, control and PC
    set $msp = *(uint32_t*) $AppAddr
    set $psp = *(uint32_t*)$AppAddr
    set $control = 0
    set $JumpAddress = *(uint32_t*)($AppAddr+4)
    set $pc = $JumpAddress
    # jump *$JumpAddress
end


define do_more
	set $i=500
    while($i>0)
        # next
        step
		set $i-=1
    end
end

define show_reg
    p/x _stm32_ospi_bus
    p/x *_stm32_ospi_bus.OSPI_Handler.Instance
end

define do_bp
  b rt_hw_board_init
  b rt_components_board_init
  b mpu_init
end

define do_func
    # display/x $sp
    set $entry_frame=$sp
    while($sp<=$entry_frame)
        #next
        step
    end
end


# b rt_sfud_flash_probe_ex
# b HAL_OSPI_Command
# b HAL_OSPI_Transmit
# b HAL_OSPI_Receive
# b ospi_send_cmd
# b ospixfer
# b rt_spi_send_then_recv
# b read_jedec_id
# b spi_write_write
# b spi_write_read
# b rt_spi_send_then_recv
# b HAL_SPI_Transmit
# commands 
#   display/x *pData@5
# end
# b HAL_SPI_Receive
# commands 
#   display/x *pData@5
# end
# b usbh_msc_thread

# b HAL_SPI_Transmit
# commands
#   do_func
# end


# 经验: 捕捉外设的State和ErrorCode的变化
# watch _stm32_ospi_bus.OSPI_Handler.State
# watch _stm32_ospi_bus.OSPI_Handler.ErrorCode
# b ospixfer
# b HAL_OSPI_Init
# b rt_qspi_configure
# b rt_spi_bus_configure
# b HAL_OSPI_Transmit_DMA
# b OCTOSPI1_IRQHandler
# b MDMA_IRQHandler
# b HAL_OSPI_TxCpltCallback
# b HAL_OSPI_Transmit_DMA
# b stm32h7xx_hal_ospi.c:1501

# display/x _stm32_ospi_bus.OSPI_Handler.State
# display/x _stm32_ospi_bus.OSPI_Handler.ErrorCode


awatch spi_bus_obj[1].handle.State
awatch spi_bus_obj[1].handle.ErrorCode
awatch spi_bus_obj[1].handle.Lock
# display/x spi_bus_obj[1].handle.Instance->SR
# b spixfer
# b drv_spi.c:449
# b HAL_SPI_Transmit
# b HAL_SPI_Receive
# b spixfer
# b rt_spi_send_then_recv

define trace_spi
    while (1)
        step
        if (spi_bus_obj[1].handle.State != HAL_SPI_STATE_READY)
            break
        end
    end
end

# display/x spi_bus_obj[1].handle.State
# display/x spi_bus_obj[1].handle.ErrorCode